import { routerRedux } from 'dva/router';
import { sendMessage, getMessageByGroup, getMessageByUserid, replyMessage, getMessageByReply } from '../services/message';
import { message } from 'antd';

export default {
  namespace: 'message',
  state: {
    isCreating: false,
    messageGroup: 1,
    messageList: [],
    messageUserid: 0,
    messageId: '',
    AdminMessageListState: 'statuslist',
    messageListState: 'messagelist'
  },

  effects: {
    *setMessageGroup({ payload }, { call, put }) {
      yield put({ type: 'save', payload});
    },

    *setMessageState({ payload }, { call, put }) {
      yield put({ type: 'save', payload});
    },

    *getMessageByUserid({ payload }, { call, put }) {
      const { messageUserid } = payload;
      console.log('getMessageByUserid');
      const res = yield getMessageByUserid(messageUserid);
      if (res && !res.err) {
        const messages = res.data.data;
        yield put({ type: 'saveList', payload: messages});
      }
    },

    *getMessageByReply({ payload }, { call, put }) {
      const { messageUserid } = payload;
      const res = yield getMessageByReply(messageUserid);
      if (res && !res.err) {
        const messages = res.data.data;
        yield put({ type: 'saveList', payload: messages});
      }
    },

    *getMessageByGroup({ payload }, { call, put }) {
      const { messageGroup } = payload;
      const res = yield getMessageByGroup(messageGroup);
      if (res || !res.err) {
        const messages = res.data.data;

        console.log(messages);
      }
    },

    *setCreated({ payload }, { call, put }) {
      yield put({ type: 'save', payload});
    },

    *new({ payload }, { call, put }) {
      const { title, content } = payload;
      const messageInfo = {
        title,
        content,
      }
      const res = yield sendMessage(messageInfo);
      if (res) {
        message.success('发送成功');
        payload = { isCreating: false };
        yield put({ type: 'save', payload});
      } else {
        message.error('发送失败');
      }
    },

    *reply({ payload }, { call, put }) {
      const { title, content, messageId } = payload;
      const messageInfo = {
        title,
        content,
        messageId
      }
      const res = yield replyMessage(messageInfo);
      if (res) {
        message.success('回复成功');
        payload = { isCreating: false };
        yield put({ type: 'save', payload});
      } else {
        message.error('回复失败');
      }
    }
  },

  reducers: {
    'save'(state, action) {
      return { ...state, ...action.payload };
    },
    'saveList'(state, action) {
      const oldList = state.messageList;
      const newList = action.payload;
      const list = newList.map(item => {
        const { messageId, messageTs, title, username, content, replyId } = item;
        const date = new Date(messageTs);
        return {
          key: messageId,
          messageId,
          username,
          title,
          content,
          replyId,
          messageTs: date.toLocaleDateString()
        }
      })
      if (JSON.stringify(oldList) == JSON.stringify(list)) {
        console.log('same!');
        return { ...state };
      } else {
        return { ...state, messageList: list };
      }
    },
  },
};
